package com.hotel.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.hotel.entity.Authority;
import com.hotel.entity.RoleOfAuthority;
import com.hotel.service.AuthorityService;
@Controller
public class AuthorityController {
	@Autowired
	private AuthorityService authorityService;
	
	/**
	 * 分页查询所有角色
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	//@RequestMapping(method = RequestMethod.POST)
	public List<Authority> findAllAuthoritys(Integer pageNo,Integer pageSize){
		List<Authority> findAllAuthoritys = authorityService.findAllAuthoritys(pageNo,pageSize);
		return findAllAuthoritys;
	}
	
	/**
	 * 分页查询所有角色并返回视图
	 * @param request
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="authorityList")
	public ModelAndView authorityList(HttpServletRequest request,@RequestParam(value="pageNo",defaultValue="1")Integer pageNo,@RequestParam(value="pageSize",defaultValue="10")Integer pageSize){
		ModelAndView view1 = new ModelAndView();
		List<Authority> authoritys = findAllAuthoritys(pageNo,pageSize);
		view1.addObject("authoritys",authoritys);
		view1.addObject("pageNo",pageNo);
		view1.addObject("pageSize", pageSize);
		view1.setViewName("views/admin/authority/authorityList");
		return view1;
	}
	
	/**
	 * 授予权限
	 * @param request
	 * @return
	 */
	@RequestMapping(value="authorityAllot")
	public ModelAndView AuthorityAllot(HttpServletRequest request){
		ModelAndView view2 = new ModelAndView();
		List<RoleOfAuthority> roleofauthoritys = authorityService.findAllRoleOfAuthoritysByUser();
		view2.addObject("roleofauthoritys", roleofauthoritys);
		view2.setViewName("views/admin/authority/authorityAllot");
		return view2;
	}
	
	/**
	 * 对权限基本信息的增删改
	 * @param authorId
	 * @param authorName
	 * @param choose
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value="author")
	@ResponseBody
	public String author(Integer authorId,String authorName,String choose){
		Authority authority = new Authority();
		authority.setPermissionName(authorName);
		//添加
		if(choose.equals("add")){
			return authorityService.insert(authority)==1?"yes":"no";
		//更新
		}else if(choose.equals("update")){
			authority.setAuthorityId(authorId);
			return authorityService.updateByPrimaryKey(authority)==1?"yes":"no";
		}else if(choose.equals("delete")){
			authority.setAuthorityId(authorId);
			return authorityService.deleteByPrimaryKey(authorId)==1?"yes":"no";
		}
		return "";
	}
	
}
